#include "xsUtils.hpp"

#include <windows.h> // sic

namespace xs
{

//=================================================================================================
// wstr_to_str
//=========

std::string wstr_to_str(const std::wstring &in)
{
	std::string out;

	if(in.empty()) return out;

	int size = WideCharToMultiByte(
		CP_ACP, 0, in.data(), (int)in.length(), NULL, 0, NULL, NULL);

	if(size==0) return out;

	char *buffer = new char[size];

	if(WideCharToMultiByte(
		CP_ACP, 0, in.data(), (int)in.length(), buffer, (int)size, NULL, NULL))
	{
		out.assign(buffer, size);
	}
	delete [] buffer;

	return out;
}

//=================================================================================================
// str_to_wstr
//=========

std::wstring str_to_wstr(const std::string &in)
{
	std::wstring out;

	if(in.empty()) return out;

	int size = MultiByteToWideChar(
    CP_ACP, 0, in.data(), (int)in.length(), NULL, 0);

	if(size==0) return out;

	wchar_t* buffer = new wchar_t[size];

	if(MultiByteToWideChar(
    CP_ACP, 0, in.data(), (int)in.length(), buffer, size))
	{
		out.assign(buffer, size);
	}

	delete [] buffer;

	return out;
}

}